#include <buola/gui.h>

#include <buola/image/cfont.h>
#include <buola/image/cgc.h>

#include <buola/gui/cconfig.h>

#include <buola/widgets/cstatusbar.h>
#include <buola/widgets/ctheme.h>

namespace buola { namespace gui {

CStatusBar::CStatusBar()
{
    mPressed=false;

    get_default_theme()->SetBackground(this);

    mFont=img::CFont::GetStock(L"control");
    SetPreferredSize({20,20});
}

CStatusBar::~CStatusBar()
{
}

void CStatusBar::ShapeAll()
{
    if(!mSBI.size()) return;

    double lSize=((int)Size().x-3)/mSBI.size();
    for(int i=0;i<mSBI.size();i++)
    {
        mSBI[i]->mSize={lSize,Size().y-1};
    }
}

void CStatusBar::PosAll()
{
    double lX=2;
    for(int i=0;i<mSBI.size();i++)
    {
        mSBI[i]->mPos={lX,1};
        lX+=mSBI[i]->mSize.x;
    }
}

void CStatusBar::DrawItem(img::CGC *pGC,CSBItem *pItem)
{
    CPoint_d lPos=pItem->mPos;
    //CSize_d lSize=pItem->mSize;

    pGC->SetSource(gConfig.mColors.Get(L"normal_text"));

//  if(pItem->mType==VALUE_STRING)
    {
        pGC->DrawText(CPoint_d(lPos.x+2,lPos.y+2),pItem->mText);
    }
}

void CStatusBar::UndrawItem(img::CGC *pGC,CSBItem *pItem)
{
    CPoint_d lPos=pItem->mPos;
    CSize_d lSize=pItem->mSize;

    pGC->SetSource(gConfig.mColors.Get(L"normal_back"));

    pGC->Path().Rect(lPos,lSize);
    pGC->FillPath();
}

///////////////// events //////////////////

void CStatusBar::OnSize(const CGeometryEvent &pE)
{
    ShapeAll();
    PosAll();
}

void CStatusBar::OnExpose(const CExposeEvent &pE)
{
    img::CGC *lGC=pE.mGC;

    get_default_theme()->DrawStatusBar(lGC,this,CTheme::DRAW_NONE);

    lGC->SetFont(mFont);
    for(int i=0;i<mSBI.size();i++)
    {
        DrawItem(lGC,mSBI[i]);
    }
}

void CStatusBar::OnButtonDown(const CMouseEvent &pE)
{
    if(pE.mButton==EMouseButton::LEFT)
    {
    }
}

void CStatusBar::OnButtonUp(const CMouseEvent &pE)
{
    if(pE.mButton!=EMouseButton::LEFT) return;
}

void CStatusBar::OnMouseMove(const CMouseEvent &pE)
{
}

void CStatusBar::OnMouseOut(const CMouseEvent &pE)
{
}

void CStatusBar::OnItemAdded(CSBItem*)
{
    ShapeAll();
    PosAll();
    Refresh();
}

void CStatusBar::OnItemChanged(CSBItem *pItem)
{
    img::CGC *lGC=StartPainting();
    lGC->SetFont(mFont);

    UndrawItem(lGC,pItem);
    DrawItem(lGC,pItem);

    EndPainting(lGC);
}

/*namespace gui*/ } /*namespace buola*/ }
